package com.ebelter.btlibrary.btble.impl.tmm;


import com.ebelter.btlibrary.btble.ble.bluetooth.device.impl.BleMessageAnalyser;
import com.ebelter.btlibrary.btble.ble.bluetooth.device.impl.Channel;
import com.ebelter.btlibrary.btble.impl.ecg.model.EcgResult;
import com.ebelter.btlibrary.btble.impl.tmm.callback.TmmCommand;
import com.ebelter.btlibrary.btble.impl.tmm.callback.TmmError;
import com.ebelter.btlibrary.btble.impl.tmm.callback.TmmResultCallback;
import com.ebelter.btlibrary.btble.impl.tmm.model.TmmResult;
import com.ebelter.btlibrary.util.ULog;

import java.util.ArrayList;
import java.util.List;

/**
 * 耳温枪数据解析器
 * Created by Sergio Pan on 2017/12/8.
 */

public class TmmMessageAnalyser extends BleMessageAnalyser {

    private static final String TAG = "TmmMessageAnalyser";

    private static TmmMessageAnalyser instance = new TmmMessageAnalyser();

    private TmmResultCallback measureResultCallback;


    private TmmMessageAnalyser() {
//        recordSet.
    }


    public static TmmMessageAnalyser getInstance() {
        return instance;
    }

    public void registerMeasureResultCallback(TmmResultCallback callback) {
        this.measureResultCallback = callback;
    }


    /**
     * 解密数据
     *
     * @param data
     * @return
     */
    @Override
    protected byte[] decrypt(byte[] data) {
//        ULog.i(TAG, "-------decrypt--------data = " + getArrayString(data));
        //TODO 如若是加密数据，在此做解密处理,如若非加密数据，可不重写此方法

        return data;
    }

    /**
     * 开始解析
     *
     * @param data
     * @return
     */
    @Override
    protected void analyze(Channel channel, byte[] data) {
        ULog.i(TAG, "-------analyze----before filter----data = " + getArrayString(data));
        //解密后
//        if (data.length < 2 || data[0] != dataHeaderReceive || data.length != (data[1] & 0xff) + 3) {
//            return;
//        }
//        ULog.i(TAG, "-------analyze---after filter-----data = " + getArrayString(data));
        //业务码

        int cmdCode = data[0] & 0xff;
        ULog.i(TAG, "-------analyze--------cmdCode = " + Integer.toHexString(cmdCode));
        switch (cmdCode) {

            case 0xff: //接收到当前测量结果
                doResult(data);
                break;
            case 0xee: //错误码
                doError(data);
                break;
            case 0xf0: //关机
                doPoweroff(data);
                break;

        }


    }


    /**
     * 收到测试结果
     *
     * @param data
     */
    private void doResult(byte[] data) {
        ULog.i(TAG, "-------doResult--------data = " + getArrayString(data));
        long measureTime = System.currentTimeMillis();
        TmmResult result = new TmmResult();
        result.setNumber(data[1]);
        int high = data[2]&0xff;
        int low = data[3]&0xff;
        ULog.i(TAG, "-------doResult--------high = " + high +", low = " + low);
        result.setTemperature(get2ByteValue(data[2], data[3])/10.0f);
        result.setMeasureTime(measureTime);
        result.setMeasureTimeStr(getMeasureTimeStr(measureTime));

        if (measureResultCallback != null) {
            measureResultCallback.onRecceiveResult(result);
        }
        if (mInternalChannel != null) {
            mInternalChannel.sendCommnd(TmmCommand.RECEIVED);
        }
    }

    /**
     * 错误码
     *
     * @param data
     */
    private void doError(byte[] data) {

        int errCmd = data[1] & 0xff;
        ULog.i(TAG, "-------doError--------errCmd = " + errCmd);

        TmmError error = null;
        switch (errCmd) {
            case 0x01:
                error = TmmError.TEMPERATURE_TOO_LOW;
                break;
            case 0x02:
                error = TmmError.TEMPERATURE_TOO_HIGH;
                break;
            case 0x03:
                error = TmmError.ENVIRONMENT_TEMPERATURE_TOO_LOW;
                break;
            case 0x04:
                error = TmmError.ENVIRONMENT_TEMPERATURE_TOO_HIGH;
                break;
            case 0x05:
                error = TmmError.POWER_TOO_LOW;
                break;
            case 0x06:
                error = TmmError.OTHER_ERR;
                break;
        }


        if (measureResultCallback != null && error != null) {
            measureResultCallback.onDeviceError(error);
        }
    }

    /**
     * 关机
     *
     * @param data
     */
    private void doPoweroff(byte[] data) {
        int poweroff = data[1] & 0xff;
        ULog.i(TAG, "-------doPoweroff--------poweroff = " + poweroff);
        if (poweroff == 0xff) {
            if (measureResultCallback != null) {
                measureResultCallback.onDevicePoweroff();
            }
        }
    }




    @Override
    public void onRelease() {
        super.onRelease();


    }
}
